package com.dhf.framework.system.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dhf.framework.base.mybatis.core.page.Pagination;
import com.dhf.framework.common.BaseController;
import com.dhf.framework.system.entity.SysRole;
import com.dhf.framework.system.entity.SysUser;
import com.dhf.framework.system.entity.SysUserRole;
import com.dhf.framework.system.service.SysRoleService;
import com.dhf.framework.system.service.SysUserRoleService;
import com.dhf.framework.system.service.SysUserService;

 /**
 * </b>SysUserRoleController<br>
 * @author HoraceHo
 * @version:1.0
 */
@Controller
@RequestMapping("/system/sysUserRole")
public class SysUserRoleController extends BaseController{
		private static Logger LOGGER = LoggerFactory.getLogger(SysUserRoleController.class);
	
  		@Autowired
  		private SysUserRoleService sysUserRoleService;
  		
  		@Autowired
  		private SysRoleService sysRoleService;
  		
  		@Autowired
  		private SysUserService sysUserService;
  		
  		/**
		  * 
		  * @Title: queryPageList
		  * @Description: 列表页面
		  * @param @param searchEntity
		  * @param @param pagination
		  * @return ModelAndView    返回类型
		  * @throws
		*/
		@RequestMapping(value = "/queryPageList")
		public ModelAndView queryPageList(SysUserRole sysUserRole, Pagination<SysUserRole> pagination) {
			LOGGER.debug(SysUserRoleController.class.getName()+":开始查询分页数据！");
			ModelAndView mv = new ModelAndView( "system/sysUserRole/sysUserRoleList" );
			pagination = this.sysUserRoleService.queryPageList( sysUserRole, pagination );
			List<SysUserRole> roleList=pagination.getItems();
			for(SysUserRole entity:roleList){
				Long roleId=entity.getRoleId();
				Long userId=entity.getUserId();
				if(roleId!=null){
					SysRole sysRole=this.sysRoleService.get(roleId);
					entity.setRoleName(sysRole.getRoleName());
				}
				if(userId!=null){
					SysUser sysUser=this.sysUserService.get(userId);
					entity.setUserName(sysUser.getName());
				}
			}
			List<SysRole> list=this.sysRoleService.query(null);
			mv.addObject( "list", list );
			mv.addObject( "pagination", pagination );
			return mv;
		}
		
		
		/**
		 * 
		 * @Title: toMerge
		 * @Description: 新增或修改页面
		 * @param @param entity
		 * @return ModelAndView    返回类型
		 * @throws
		 */
		@RequestMapping(value = "/toMerge")
		public ModelAndView toMerge(SysUserRole entity){
			ModelAndView mv = new ModelAndView("system/sysUserRole/sysUserRoleMerge");
			if(entity.getId()!= null){
				SysUserRole sysUserRole =  this.sysUserRoleService.get( entity.getId() ) ;
				mv.addObject("sysUserRole",sysUserRole);
			}else{
				mv.addObject("sysUserRole", new SysUserRole());
			}
			List<SysRole> roleList=this.sysRoleService.query(null);
			mv.addObject( "roleList", roleList );
			List<SysUser> userList=this.sysUserService.query(null);
			mv.addObject( "userList", userList );
			return mv;
		}

		/**
		 * 
		* @Title: doMerge
		* @Description: 新增或修改数据 
		* @param @param request
		* @param @param entity
		* @return ModelAndView    返回类型
		* @throws
		 */
		@RequestMapping(value = "/doMerge")
		public ModelAndView doMerge(HttpServletRequest request, SysUserRole entity,RedirectAttributes redirectAttributes) {
			if (entity.getId()!=null) {
				LOGGER.debug(SysUserRoleController.class.getName()+":开始更新数据！");
				this.addMessage(redirectAttributes, "修改数据成功");
				this.sysUserRoleService.update( entity );
			} else {
				LOGGER.debug(SysUserRoleController.class.getName()+":开始新增数据！");
				this.sysUserRoleService.add( entity );
				this.addMessage(redirectAttributes, "新增据成功");
			}
			return new ModelAndView( "redirect:/system/sysUserRole/queryPageList.do" );
		}

		/**
		 * 
		 * @Title: delete
		 * @Description:删除数据
		 * @param @param id
		 * @return ModelAndView    返回类型
		 * @throws
		 */
		@RequestMapping(value = "/delete")
		public ModelAndView delete(Long id,RedirectAttributes redirectAttributes) {
			LOGGER.debug(SysUserRoleController.class.getName()+":开始删除数据！");
	        this.sysUserRoleService.delete( id );
	        this.addMessage(redirectAttributes, "删除数据成功");
	        return new ModelAndView( "redirect:/system/sysUserRole/queryPageList.do" );
		}
}

